Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solves issue #607 #608

Merged
merged 4 commits into from
Sep 19, 2024
Merged

Solves issue #607 #608

merged 4 commits into from
Sep 19, 2024

Conversation

MolbioUnige
Copy link
Contributor

Solves problem induced by cake v5.1

@@ -55,7 +56,7 @@ public function initialize(array $config): void
$schema = $this->_table->getSchema();
/** @var string $field */
foreach (array_keys($this->getConfig()) as $field) {
$schema->setColumnType($field, 'upload.file');
$schema->addColumn($field, ['type' => 'upload.file']);
Copy link
Member

@ADmad ADmad Sep 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A much simpler solution is just wrapping the setColumnType() call within a hasColumn() check, which won't require any additional changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just saw your suggestion, I'll try that

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fact, I don't think so. Those fields are "virtual". They don't reside in the table. hasColum will always return false

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it would and hence nothing needs to be done. In 5.0 the setColumnType() call simply returned without doing anything if the column did not actually exist in the schema. So things remain the same.

Copy link
Contributor Author

@MolbioUnige MolbioUnige Sep 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In 5.0, it would add the non-existant column in the schema, or I did something wrong

object(Cake\Database\Schema\TableSchema) id:0 {
'table' => 'attachments'
'columns' => [
    'id' => [ ],
    'name' => [ ],
    'size' => [ ],
    'type' => [ ],
    'dir' => [ ],
    'created' => [ ],
    'modified' => [ ],
    'email_heading' => [ ],
    'thumbnail' => [ ],
    'poster' => [ ],
    'illustration' => [ ],
]

email_heading, thumbnail, poster and illustration here above are the attachments defined

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In 5.0, it would add the non-existant column in the schema...

No it doesn't https://github.com/cakephp/cakephp/blob/5.0.11/src/Database/Schema/TableSchema.php#L379-L383

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah yes, sorry, I didn't clear the cache after having downgraded to 5.0.

Copy link

codecov bot commented Sep 18, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.80%. Comparing base (324dc73) to head (d2d6367).
Report is 1 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master     #608      +/-   ##
============================================
- Coverage     95.84%   95.80%   -0.05%     
- Complexity      109      110       +1     
============================================
  Files            11       11              
  Lines           289      286       -3     
============================================
- Hits            277      274       -3     
  Misses           12       12              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -71,7 +71,7 @@ public function testInitialize()
->onlyMethods(['setColumnType'])
->disableOriginalConstructor()
->getMock();
$schema->expects($this->once())
$schema->expects($this->any())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of changing this add the field column to the schema.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean adding the field column in tests/schema.php? Doing this does nothing by itself, and test are failing:

  1. Josegonzalez\Upload\Test\TestCase\Model\Behavior\UploadBehaviorTest::testInitialize
    Expectation failed for method name is "setColumnType" when invoked 1 time.
    Method was expected to be called 1 time, actually called 0 times.

What I understand is that, as the schema is a mock, I have to mock the 'hasColumn' method as well, and return true.

$schema = $this->getMockBuilder('Cake\Database\Schema\TableSchema')
   ->onlyMethods(['setColumnType', 'hasColumn'])
   ->disableOriginalConstructor()
   ->getMock();
$schema->expects($this->once())
   ->method('hasColumn')
   ->with('field')
   ->willReturn(true);

The test then passes, but then I don't see the point of modifying the schema in that case, or is there something else that I'm missing?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes you'll need to add expects for hasColum() too.

We need to separately test for the two cases where the column is in the schema and when it's not and not use any() as it's less work :)

@ADmad ADmad merged commit 90675b2 into FriendsOfCake:master Sep 19, 2024
7 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants